#include "stdafx.h"
#include "svrhandlermgr.h"
#include "ISProto.h"
#include "gshandler.h"
#include "rolemgr.h"
#include "trademgr.h"
#include "mailmgr.h"
#include "itemdb.h"

IMPLEMENT_SINGLETON(CSvrHandlerMgr)

CSvrHandlerMgr::CSvrHandlerMgr()
{
    m_poBaseSvrMgr = NULL;    
}

CSvrHandlerMgr::~CSvrHandlerMgr()
{

}

bool CSvrHandlerMgr::Init(ISDBaseSvrMgr * poBaseSvrMgr)
{
    m_poBaseSvrMgr = poBaseSvrMgr;

    if (false == CGSHandler::CreateInstance())
    {
        SYS_CRITICAL("CGSHanlder: CreateInstance: failed");
        return false;
    }

    if (false == CRoleMgr::CreateInstance())
    {
        SYS_CRITICAL("CRoleMgr: CreateInstance: failed");
        return false;
    }

    if (false == CTradeMgr::CreateInstance())
    {
        SYS_CRITICAL("CGSHanlder: CreateInstance: failed");
        return false;
    }

    if (false == CMailMgr::CreateInstance())
    {
        return false;
    }

    if (false == CItemDB::CreateInstance())
    {
        return false;
    }

    if (false == CItemDB::Instance()->Init())
    {
        return false;
    }

    return true;
}

ISDSvrHandler* CSvrHandlerMgr::GetSvrHandler(UINT8 bySvrType)
{
    if (SD_GAME_SERVER == bySvrType)
    {
        return CGSHandler::Instance();
    }
    
    return NULL;
}

bool CSvrHandlerMgr::Run(void)
{
    CGSHandler::Instance()->Run();
    return true;
}

void CSvrHandlerMgr::Release(void)
{
    CItemDB::DestroyInstance();
    CRoleMgr::DestroyInstance();
    CTradeMgr::DestroyInstance();
    CMailMgr::DestroyInstance();
    CGSHandler::DestroyInstance();
}
    